#!/usr/bin/env python
# -*- coding: utf-8 -*-

from Tkinter import*

def frame (root, side):
	w = Frame(root)
	w.pack(side = side , expand = YES , fill= BOTH)
	return w

def button(root,side,text, command = None):
	w = Button(root, text=text , command=command)
	w.pack(side=side, expand=YES, fill=BOTH)
	return w

class Calculator(Frame):
	
	def __init__(self):
		Frame.__init__(self)
		self.pack(expand=YES, fill=BOTH)
		#nome da janela
		self.master.title('Calculadora')
		#nome do icon
		self.master.iconname('calcul1')
		#criacao do display e de uma var de acesso ao conteudo widget
		display = StringVar()
		Entry(self, relief=SUNKEN, textvariable=display).pack(side=TOP,expand=YES,fill=BOTH)
		# cria-se uma Frame para cada linha de digitos
		
		for key in ("123", "456" , "789", "-0."):
			keyF = frame(self, TOP)
			# para cada digito cria-se um botao
			for char in key:
				button(keyF,LEFT, char, lambda w=display, s ='%s ' % char : w.set(w.get()+s))
				opsF = frame(self, TOP)
		for char in "+-*/=":
					# associacao dos operadores a botoes
			if char == '=':
				btn = button(opsF, LEFT , char)
				btn.bind ('<ButtonRelease-1>', lambda e, s = self, w=display : s.calc(w), '+')
			else:
				btn = button(opsF , LEFT, char , lambda w=display, c=char : w.set(w.get()+' ' + c + ' '))
						
		clearF = frame(self, BOTTOM)
		button(clearF, LEFT, 'Clr' ,lambda w=display: w.set(''))
		
	def calc(self, display):
			#eval permite executar codigo por chamada ao interpretador
		try:
			display.set(eval(display.get()))
		except ValueError:
			display.set("ERROR")

# iniciar app

if __name__ == '__main__':
	Calculator().mainloop()			
	
		
